const mybehavior = require("../../../../behaviors/requests")
// packageB/pages/package/workbench/workbench.js
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    _id: "",
    company: "",
    ID_number: wx.getStorageSync('ID_number'),
    baseUrl: "https://logisticsengineering.gree.com/package_integrated_api",
    baseUrl1: "https://logisticsengineering.gree.com",
    manager: "",
    phone_num: wx.getStorageSync('phone_num'),
    role: "",
    data_auth: "",
    self_info_show2: false,
    self_info: {},
    yam_btn_disabled: false,
    yam_btn: "primary",
    yam_word: "发送",
    count: 0,
    new_ver_code: "",
    new_phonenum: "",
    new_name: "",
    new_ID_number: "",
    name: wx.getStorageSync('name'),
    daiban_num: 10,
    user_info: wx.getStorageSync('name'),
    sqd_total: 0,
    total1: 0,
    total: 0,
    now_time: "",
    self_info_show3: false,
    block_show: false,
    // 列表数据
    dataList: [
      {
        content: "工装容器管理",
        class: "container_class",
        checked: "container_class_show",
        status: true
      },
      {
        content: "周转物资进出门",
        class: "package_class",
        checked: "package_class_show",
        status: true
      },
      {
        content: "工艺标准管理",
        class: "pfep_class",
        checked: "pfep_class_show",
        status: true
      },
      {
        content: "亮灯拣选",
        class: "liangdeng_class",
        checked: "liangdeng_class_show",    
        status: true
      },
      {
        content: "智能物料柜",
        class: "smart_box_class",
        checked: "smart_box_class_show",
        status: true
      },
      {
        content: "物流车辆调度",
        class: "wuliucheliangdiaodu_class",
        checked: "wuliucheliangdiaodu_class_show",
        status: true
      },
      {
        content: "RFID配送管理",
        class: "rfid_class",
        checked: "rfid_class_show",
        status: true
      },
      {
        content: "电子标签检测",
        class: "electronic_tag_class",
        checked: "electronic_tag_class_show",
        status: true
      },
      {
        content: "Andon拉动配送",
        class: "andon_class",
        checked: "andon_class_show",
        status: true
      },
      {
        content: "AGV调度管理",
        class: "agv_class",
        checked: "agv_class_show",
        status: true
      },




    ],
    // 移动块的id
    moveId: null,
    // 最终停止的位置
    endY: 0,
    block_show_obj: {
      container_class_show: true,
      package_class_show: true,
      pfep_class_show: true,
      liangdeng_class_show: true,
      smart_box_class_show: true,
      wuliucheliangdiaodu_class_show: true,
      rfid_class_show: true,
      electronic_tag_class_show: true,
      andon_class_show: true,
      agv_class_show: true,
    }

  },
  onChange(e) {
    console.log(e);

    let index = e.currentTarget.dataset.key.id
    let key = e.currentTarget.dataset.key.checked
    let arr = JSON.parse(JSON.stringify(this.data.dataList))
    let obj = JSON.parse(JSON.stringify(this.data.block_show_obj))
    obj[key] = e.detail
    arr[index].status = e.detail
    // 需要手动对 checked 状态进行更新
    wx.setStorageSync('view_data_list', arr)
    wx.setStorageSync('block_show_obj', obj)
    this.setData({
      block_show_obj: obj,
      dataList: arr
    });
  },
  // 添加常用版块
  addBlock() {
    this.setData({
      block_show: true,

    })
  },
  onBlockClose() {
    this.setData({
      block_show: false,

    })
  },
  onBlockDone() {
    this.setData({
      block_show: false,

    })
  },

  // 重置列表顺序
  init(dataList) {
    // 列表项高度
    const ITEM_HEIGHT = 40
    // 列表项上外边距
    const ITEM_MARGIN_TOP = 14
    let newDataList = dataList.map((item, index) => {
      item.id = index
      // 重置单项的y坐标(组件默认是绝对定位 left:0  top:0 )
      item.y = (ITEM_HEIGHT + ITEM_MARGIN_TOP) * index + ITEM_MARGIN_TOP
      return item
    })
    wx.setStorageSync('view_data_list', newDataList)
    for (let i in newDataList) {

      this.setData({
        [newDataList[i].class]: "box" + (newDataList[i].id + 1)
      })
    }
    this.setData({
      dataList: newDataList
    })
  },
  // 移动中-获取移动元素的id,实时获取移动元素的y坐标
  moving(e) {
    this.setData({
      moveId: e.currentTarget.dataset.moveid,
      endY: e.detail.y,
    })
  },
  // 移动后
  moved() {
    console.log("点击");
    let {
      dataList,
      moveId,
      endY
    } = this.data
    let newDataList = JSON.parse(JSON.stringify(dataList))
    newDataList[moveId].y = endY
    newDataList = newDataList.sort((a, b) => a.y - b.y)
    this.init(newDataList)

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let self = this
    let lists = wx.getStorageSync('view_data_list')
    if (lists) {
      self.init(lists)
    } else {
      self.init(this.data.dataList)
    }
    let obj = wx.getStorageSync('block_show_obj')
      if (obj) {
      self.setData({
        block_show_obj: obj
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {



  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})